package com.hk.webapp.controller.admin;

import com.hk.webapp.res_config.FileUploadConfig;
import com.hk.webapp.annotations.AllowAnonymous;
import com.hk.webapp.res_config.OssConfig;
import com.hk.webapp.utils.FileUploadUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
public class UeditorController extends AdminBaseController {

    @Resource
    private FileUploadConfig fileUploadConfig;
    @Resource
    private OssConfig ossConfig;

    @GetMapping("/Ueditor/Index")
    @AllowAnonymous
    public Object Index(String act){

        switch (act){
            case "config":
                String s="{\"imageActionName\":\"uploadimage\",\"imageFieldName\":\"upfile\",\"imageMaxSize\":2048000,\"imageAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"imageCompressEnable\":true,\"imageCompressBorder\":1600,\"imageInsertAlign\":\"none\",\"imageUrlPrefix\":\"\",\"imagePathFormat\":\"/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\"scrawlActionName\":\"uploadscrawl\",\"scrawlFieldName\":\"upfile\",\"scrawlPathFormat\":\"/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\"scrawlMaxSize\":2048000,\"scrawlUrlPrefix\":\"\",\"scrawlInsertAlign\":\"none\",\"snapscreenActionName\":\"uploadimage\",\"snapscreenPathFormat\":\"/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\"snapscreenUrlPrefix\":\"\",\"snapscreenInsertAlign\":\"none\",\"catcherLocalDomain\":[\"127.0.0.1\",\"localhost\",\"img.baidu.com\"],\"catcherActionName\":\"catchimage\",\"catcherFieldName\":\"source\",\"catcherPathFormat\":\"/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\"catcherUrlPrefix\":\"\",\"catcherMaxSize\":2048000,\"catcherAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"videoActionName\":\"uploadvideo\",\"videoFieldName\":\"upfile\",\"videoPathFormat\":\"/ueditor/php/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}\",\"videoUrlPrefix\":\"\",\"videoMaxSize\":102400000,\"videoAllowFiles\":[\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\"],\"fileActionName\":\"uploadfile\",\"fileFieldName\":\"upfile\",\"filePathFormat\":\"/ueditor/php/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}\",\"fileUrlPrefix\":\"\",\"fileMaxSize\":51200000,\"fileAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\",\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\",\".rar\",\".zip\",\".tar\",\".gz\",\".7z\",\".bz2\",\".cab\",\".iso\",\".doc\",\".docx\",\".xls\",\".xlsx\",\".ppt\",\".pptx\",\".pdf\",\".txt\",\".md\",\".xml\"],\"imageManagerActionName\":\"listimage\",\"imageManagerListPath\":\"/ueditor/php/upload/image/\",\"imageManagerListSize\":20,\"imageManagerUrlPrefix\":\"\",\"imageManagerInsertAlign\":\"none\",\"imageManagerAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"fileManagerActionName\":\"listfile\",\"fileManagerListPath\":\"/ueditor/php/upload/file/\",\"fileManagerUrlPrefix\":\"\",\"fileManagerListSize\":20,\"fileManagerAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\",\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\",\".rar\",\".zip\",\".tar\",\".gz\",\".7z\",\".bz2\",\".cab\",\".iso\",\".doc\",\".docx\",\".xls\",\".xlsx\",\".ppt\",\".pptx\",\".pdf\",\".txt\",\".md\",\".xml\"]}";
                return JSON.parseObject(s);
            case "listimage":
                return "";
            case "listfile":
                return "";
            default:
                break;
        }
        return "";
    }

    @PostMapping("/Ueditor/Index")
    @AllowAnonymous
    public Object post(@RequestParam(value = "upfile", required = false) MultipartFile file, HttpServletResponse response, HttpServletRequest request, String act){
        switch (act){
            case "uploadimage":
                if (file.isEmpty()) {
                    return JSON.parseObject("{\"state\":\"FAILED\"}");
                }
                String fileUriPath = FileUploadUtils.uploadFileByLocal(fileUploadConfig, file);
                HashMap<String,Object> map=Maps.newHashMap();
                map.put("state","SUCCESS");
                map.put("size",file.getSize());
                map.put("original",file.getOriginalFilename());
                map.put("title",fileUriPath);
                map.put("type",file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1));
                map.put("url",fileUriPath);
                return map;
            case "uploadfile":
                if (file.isEmpty()) {
                    return JSON.parseObject("{\"state\":\"FAILED\"}");
                }
                String fileUriPath2 = FileUploadUtils.uploadFileByLocal(fileUploadConfig, file);
                HashMap<String,Object> map2=Maps.newHashMap();
                map2.put("state","SUCCESS");
                map2.put("size",file.getSize());
                map2.put("original",file.getOriginalFilename());
                map2.put("title",fileUriPath2);
                map2.put("type",file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1));
                map2.put("url",fileUriPath2);
                return map2;
            default:
                break;
        }
        return "";
    }
}
